{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.26\n",
      "1.27\n",
      "   涨停板  跌停价  涨停价\n",
      "0   1.265    1.26    1.26\n",
      "1   1.375    1.38    1.38\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from decimal import Decimal, ROUND_HALF_UP\n",
    "# 股票的价格是1.15,今日涨停上涨10%,涨停价格1.265。\n",
    "x = 1.265\n",
    "# rounding=\"ROUND_HALF_UP\" # 四舍五入模式; Decimal(\"1.\")代表精确到整数位, 精确到十分位填:0.1, 百分十填0.01\n",
    "print(float(Decimal(x).quantize(Decimal('1.00'), rounding=ROUND_HALF_UP)))\n",
    "# float单精度,在内存中应该是1个符号位, 8个指数位和23个有效数据位。而2~23~10”7, 由此得到 1e-7.\n",
    "print(float(Decimal(x + 1e-7).quantize(Decimal('1.00'), rounding=ROUND_HALF_UP)))\n",
    "\n",
    "# 示例DataFrame\n",
    "data = {'涨停板': [1.265, 1.375], '跌停价': [1.265, 1.375]}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 应用四舍五入\n",
    "df['涨停价'] = df['涨停板'].apply(lambda x: float(Decimal(x).quantize(Decimal('1.00'), rounding=ROUND_HALF_UP)))\n",
    "df['跌停价'] = df['跌停价'].apply(lambda x: float(Decimal(x).quantize(Decimal('1.00'), rounding=ROUND_HALF_UP)))\n",
    "\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.4420000000000006 2.44\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from decimal import Decimal, ROUND_HALF_UP\n",
    "\n",
    "# 股票的价格是1.15,今日涨停上涨10%,涨停价格1.265。\n",
    "x = 2.22*1.1\n",
    "# float单精度,在内存中应该是1个符号位, 8个指数位和23个有效数据位。而2~23~10”7, 由此得到 1e-7.\n",
    "y = (float(Decimal(x + 1e-7).quantize(Decimal('1.00'), rounding=ROUND_HALF_UP)))\n",
    "print(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\ProgramData\\Anaconda3\\lib\\site-packages\\akshare\\__init__.py:2818: UserWarning: 为了支持更多特性，请将 Pandas 升级到 2.2.0 及以上版本！\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import backtrader as bt\n",
    "import pandas as pd\n",
    "from data_feed import StockDataFetcher, MyStockData, etf_daily\n",
    "from MyStrategy import Gain10, BacktestConfig\n",
    "\n",
    "\n",
    "symbol=\"002713\"\n",
    "adjust = \"qfq\"\n",
    "start_date = datetime(2023, 10, 1)\n",
    "end_date = datetime(2024, 6, 28)\n",
    "\n",
    "etf_fetcher = StockDataFetcher(symbol=symbol,adjust=adjust) # 抓取股票行情类实例化\n",
    "data_stock = etf_fetcher.get_data()          # 对象的获取数据方法\n",
    "#data = MyStockData(dataname=data_stock, fromdate=start_date, todate=end_date) # 实例数据源 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2014-02-19</th>\n",
       "      <td>2014-02-19</td>\n",
       "      <td>5.54</td>\n",
       "      <td>7.12</td>\n",
       "      <td>7.12</td>\n",
       "      <td>5.54</td>\n",
       "      <td>6812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-02-20</th>\n",
       "      <td>2014-02-20</td>\n",
       "      <td>8.06</td>\n",
       "      <td>8.06</td>\n",
       "      <td>8.06</td>\n",
       "      <td>8.06</td>\n",
       "      <td>9985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-02-21</th>\n",
       "      <td>2014-02-21</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.10</td>\n",
       "      <td>10693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-02-24</th>\n",
       "      <td>2014-02-24</td>\n",
       "      <td>10.25</td>\n",
       "      <td>10.25</td>\n",
       "      <td>10.25</td>\n",
       "      <td>10.25</td>\n",
       "      <td>37245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-02-25</th>\n",
       "      <td>2014-02-25</td>\n",
       "      <td>10.73</td>\n",
       "      <td>11.51</td>\n",
       "      <td>11.51</td>\n",
       "      <td>10.51</td>\n",
       "      <td>200638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-28</th>\n",
       "      <td>2024-06-28</td>\n",
       "      <td>2.25</td>\n",
       "      <td>2.20</td>\n",
       "      <td>2.30</td>\n",
       "      <td>2.19</td>\n",
       "      <td>81891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-01</th>\n",
       "      <td>2024-07-01</td>\n",
       "      <td>2.22</td>\n",
       "      <td>2.22</td>\n",
       "      <td>2.27</td>\n",
       "      <td>2.16</td>\n",
       "      <td>91889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-02</th>\n",
       "      <td>2024-07-02</td>\n",
       "      <td>2.29</td>\n",
       "      <td>2.44</td>\n",
       "      <td>2.44</td>\n",
       "      <td>2.20</td>\n",
       "      <td>90921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-03</th>\n",
       "      <td>2024-07-03</td>\n",
       "      <td>2.51</td>\n",
       "      <td>2.68</td>\n",
       "      <td>2.68</td>\n",
       "      <td>2.37</td>\n",
       "      <td>284468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-04</th>\n",
       "      <td>2024-07-04</td>\n",
       "      <td>2.95</td>\n",
       "      <td>2.95</td>\n",
       "      <td>2.95</td>\n",
       "      <td>2.95</td>\n",
       "      <td>55565</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2491 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 date   open  close   high    low  volume\n",
       "date                                                     \n",
       "2014-02-19 2014-02-19   5.54   7.12   7.12   5.54    6812\n",
       "2014-02-20 2014-02-20   8.06   8.06   8.06   8.06    9985\n",
       "2014-02-21 2014-02-21   9.10   9.10   9.10   9.10   10693\n",
       "2014-02-24 2014-02-24  10.25  10.25  10.25  10.25   37245\n",
       "2014-02-25 2014-02-25  10.73  11.51  11.51  10.51  200638\n",
       "...               ...    ...    ...    ...    ...     ...\n",
       "2024-06-28 2024-06-28   2.25   2.20   2.30   2.19   81891\n",
       "2024-07-01 2024-07-01   2.22   2.22   2.27   2.16   91889\n",
       "2024-07-02 2024-07-02   2.29   2.44   2.44   2.20   90921\n",
       "2024-07-03 2024-07-03   2.51   2.68   2.68   2.37  284468\n",
       "2024-07-04 2024-07-04   2.95   2.95   2.95   2.95   55565\n",
       "\n",
       "[2491 rows x 6 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_stock"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
